package com.wf.course.web.controller.ad;

import com.utils.page.PageBean;
import com.wf.course.model.comm.ActivityImg;
import com.wf.course.service.IActivityImgService;
import com.wf.course.web.Constants;
import com.wf.course.web.Interceptor.LogInfo;
import com.wf.course.web.OSSUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 活动图片
 * Created by chenli on 2017/3/22.
 */
@Controller
@RequestMapping("activityImg")
public class ActivityImgController {

    @Autowired
    private IActivityImgService adImgService;
    /***
     * 图片管理
     * @param pageNo
     * @param pageSize
     * @param imgName
     * @param model
     * @return
     */
    @LogInfo("查询图片信息列表")
    @RequestMapping("activityImgList")
    public String imgList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                          @RequestParam(value = "pageSize", required = false) Integer pageSize,
                          @RequestParam(value = "imgName", required = false) String imgName,
                          Model model){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("imgName",imgName);

        PageBean<ActivityImg> pageBean = new PageBean<>(pageNo, pageSize,null,map);
        adImgService.getImgsList(pageBean);
        pageBean.setUrl("/activityImg/activityImgList");
        model.addAttribute("page", pageBean);
        return "comm/activityImgList";
    }

    /**
     * 图片：新增修改页面信息加载
     * @param id
     * @param model
     * @return
     */
    @LogInfo("新增/修改图片信息，页面数据初始化")
    @RequestMapping("downLoadActivityImg/{id}")
    public String downLoadImg(@PathVariable("id") Integer id, Model model){
        if(id!=null && id!=0){
            ActivityImg img = this.adImgService.downLoadImg(id);
            model.addAttribute("img", img);
            model.addAttribute("type", "upd");
        }else{
            model.addAttribute("type", "add");
        }

        return "comm/activityImgEdit";
    }


    /**
     * 添加或修改
     * @param img
     * @param imgFile
     * @param request
     * @return
     */
    @LogInfo("添加或修改图片信息")
    @RequestMapping(value = {"activityImgEdit" } )
    public String imgEdit(@ModelAttribute ActivityImg img,
                          @RequestParam(value = "imgFile", required = false)MultipartFile imgFile,
                          HttpServletRequest request){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        img.setCreateTime(sdf.format(new Date()));

        //添加信息图片
        if(img.getId()==null){
            if (!((MultipartFile) imgFile).isEmpty()) {
                String newsUrl = "";
                try {
                    newsUrl = OSSUtil.addFile(imgFile, "eval", Constants.IGM_BUCKET);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                img.setImgUrl(Constants.OSSURL+newsUrl);
            }
            //保存
            adImgService.saveImg(img);
        }else{
            //修改
            //更新logoUrl
            if (imgFile.getSize() != 0) {
                ActivityImg imga = adImgService.downLoadImg(img.getId());
                try{
                    if(StringUtils.isNotBlank(imga.getImgUrl())){
                        // 删除原来图片
                        OSSUtil.delete(imga.getImgUrl(), Constants.IGM_BUCKET);
                    }
                }catch(Exception e){
                    System.out.println(e.getMessage());
                }
                // 重新上传
                String logoUrl = "";
                try {
                    logoUrl = OSSUtil.addFile(imgFile, "eval", Constants.IGM_BUCKET);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                img.setImgUrl(Constants.OSSURL+logoUrl);
            }
            //修改
            adImgService.updateImg(img);
        }

        return "redirect:/activityImg/activityImgList";
    }

    /**
     * 删除图片
     * @param id
     * @return
     */
    @LogInfo("删除图片信息")
    @RequestMapping(value="deleteActivityImg/{id}"  )
    public String deleteImg(@PathVariable Integer id){
        this.adImgService.deleteImg(id);

        return "redirect:/activityImg/activityImgList";
    }
}
